<template>
  <div class="ydd-login">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>优点达资讯系统</span>
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="ruleForm"
        auto-complete="on"
        :model="userForm"
        :rules="rules"
      >
        <el-form-item
          label-width="auto"
          size="small"
          class="login-name"
          prop="login_name"
        >
          <el-input
            v-model="userForm.login_name"
            placeholder="账号"
            prefix-icon="el-icon-s-custom"
            @focus="removeName = true"
          />
          <span v-show="removeName" class="show-name" @click="clearName">
            <i class="el-icon-circle-close" />
          </span>
        </el-form-item>
        <el-form-item
          label-width="auto"
          size="small"
          class="pwd"
          prop="password"
        >
          <el-input
            :key="passwordType"
            ref="password"
            v-model="userForm.password"
            :type="passwordType"
            name="password"
            placeholder="密码"
            prefix-icon="el-icon-s-goods"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <el-form-item size="small">
          <el-button type="primary" :loading="loading" @click="goLogin">Login</el-button>
        </el-form-item>
      </el-form>

      <div class="tips">
        <span style="margin-right: 20px">账号：admin</span>
        <span>密码：111111</span>
      </div>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      // 定义登录数据
      userForm: {
        login_name: 'admin',
        password: '111111'
      },
      loading: false,
      // 密码图标
      passwordType: 'password',
      // 控制清除账号按钮
      removeName: false,
      // 表单验证
      rules: {
        login_name: [
          {
            required: true,
            message: '账号不能为空',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 6,
            max: 16,
            message: '密码长度必须6-16个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  computed: {},
  created() {},
  methods: {
    // 点击清除按钮，清空输入框
    clearName() {
      this.userForm.login_name = ''
      this.removeName = false
    },
    // 密码显示隐藏
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    async goLogin() {
      // 用于表单校验，// this.$refs.ruleForm.validate() 返回的是一个promise对象
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        this.loading = true
        this.$store.dispatch('user/login', this.userForm).then(() => {
          //! 获取退出登录时页面携带的参数，再次登录时，跳转到退出时页面
          const routingAddress = this.$route.query
          if (Object.keys(routingAddress).length > 0) {
            this.$router.push(routingAddress.redirect)
          } else {
            this.$router.push('/')
          }
          this.$message.success('登录成功')
        }).finally(() => {
          this.loading = false
        })
      })
      // try {
      //   await this.$refs.ruleForm.validate()

      //   this.$router.push('/')
      // } catch (error) {
      //   this.loading = false
      //   return console.log(error)
      // }
    }
  }
}
</script>
<style lang="scss" scoped>
.ydd-login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100%;
  background: url("../../assets/images/loginbg.png") no-repeat center / cover;
  .clearfix {
    font-size: 26px;
    color: coral;
    padding-bottom: 10px;
    text-align: center;
    font-weight: 400;
    line-height: 26px;
  }
  .el-form {
    width: 400px;
    .el-button {
      width: 100%;
      background: coral;
      border: 0;
    }
    .pwd {
      position: relative;
      .show-pwd {
        position: absolute;
        right: 10px;
        top: 4px;
        font-size: 16px;
        user-select: none;
      }
    }
    .login-name {
      position: relative;
      .show-name {
        position: absolute;
        right: 10px;
        top: 2px;
        font-size: 16px;
        user-select: none;
      }
    }
  }
  .tips {
    font-size: 14px;
    color: coral;
  }
}
</style>
